package cn.ly.core.concurrent.thread;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class HashMapTest {
	private Date dateKey = new Date();
	private Map<Date,String> map = new HashMap<Date,String>();
	
	public Map<Date,String> getMap(){
		return map;
	}
	
	public Date getDateKey(){
		return dateKey;
	}
	
	public static void main(String[] args) {
		HashMapTest hashTest = new HashMapTest();
		Map<Date,String> m = hashTest.getMap();
		Date d = hashTest.getDateKey();
		m.put(d, "key is " + d);
		System.out.println("value:"+m.get(d));
		
		Thread t = new Thread1(hashTest);
		t.start();
		
		while(t.isAlive()){
			Thread.yield();//使当前线程交出cpu执行权，到就绪状态，cpu重新随机挑选下一个线程执行，有可能再次选择刚刚停止的线程
		}
		//t.join();阻塞线程 t；当调用它的线程执行完毕后，t 再绩效向下执行。

		System.out.println("value:"+m.get(d));
		
	}

}

class Thread1 extends Thread{
	private HashMapTest h;
	public Thread1(HashMapTest h){
		this.h = h;
	}
	
	public void run(){
		Date d = h.getDateKey();
		d.setTime(new Date().getTime()+1);//使用Date类型作为hashmap的key，对象状态有可能发生变化，导致内存泄露
	}
}
